import { Component, ViewChild } from '@angular/core';
import { IonicPage, NavController, NavParams, AlertController} from 'ionic-angular';
import { Slides, Content  } from 'ionic-angular';

//提货申请
import { TihuoApplyPage } from '../tihuo-apply/tihuo-apply';


import { Https } from '../../app/http.service'
/**
 * Generated class for the CardPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
    selector: 'page-card',
    templateUrl: 'card.html',
})
export class CardPage {
    @ViewChild(Slides) slides: Slides;
    @ViewChild(Content) content: Content;
    Url: string = new Https().Url + "act=goods&op=app_my_card"
    data=[];
    card= [];
    //tab列表
    segmentsArray = ['kittens', 'mobile','jd', 'unicom', 'telcom'];
    //选中
    segmentModel: string = this.segmentsArray[0];
    currentIndex:number
    constructor(public navCtrl: NavController, public navParams: NavParams, public alertCtrl: AlertController) {
    }

    ionViewDidEnter() {
        
        console.log('ionViewDidLoad CardPage');
        fetch(this.Url,{ 
            method: 'post',
            headers:{
                'Content-Type': 'application/x-www-form-urlencoded'
            },
            body: "mid=" + localStorage.getItem("mid") + "&token=" + localStorage.getItem("token"),
        }).then((responsce)=>{
            responsce.text().then((data)=>{
                let resource = JSON.parse(data).datas;
                if(resource){
                    this.data['all'] = resource.all;
                    this.card['all'] = resource.all == undefined ? 0 : 1;
                    this.data['mobile'] = resource.mobile;
                    this.card['mobile'] = resource.mobile == undefined ? 0 : 1;
                    this.data['jd'] = resource.jd;
                    this.card['jd'] = resource.jd == undefined ? 0 : 1;
                    this.data['unicom'] = resource.unicom;
                    this.card['unicom'] = resource.unicom == undefined ? 0 : 1;
                    this.data['telcom'] = resource.telcom;
                    this.card['telcom'] = resource.telcom == undefined ? 0 : 1;
                }
                
                console.log(resource)
                console.log(this.card)
            })
        }).catch((err)=>{
            let confirm = this.alertCtrl.create({ 
                title: '温馨提示',//
                message: err,
                buttons: [
                    { text: '确定' },

                ]
            });
            confirm.present();
        })
        this.currentIndex = 0
    } 
    /* 
      slide改变index触发
    */
    slideChanged(slids) {
        
        // console.log(slids._slides);
        //获取当前页面index
        let currentIndex = this.slides.getActiveIndex() > 4 ? 4 : this.slides.getActiveIndex();
        this.currentIndex = currentIndex;
        //回到顶部
        this.content.scrollTo(0, 0, 0); 
        this.segmentModel = this.segmentsArray[currentIndex];

        //获取slide子元素
        let brother = slids.container.children[0].children;

        for (let i = 0; i < brother.length; i++) {
            if (i != currentIndex) {
                if (brother[i].children[0].children[0].offsetHeight > brother[currentIndex].children[0].children[0].offsetHeight) {
                    brother[i].children[0].children[0].style = "height:" + brother[currentIndex].children[0].children[0].offsetHeight + "px"
                }
            } else {
                brother[i].children[0].children[0].style = "height:unset"
            }

        }

    }
    /* 
      点击tab定位
    */
    swipeEvnet(event) {
        //跳转到指定页面
        //获取ID定位 
        
        this.slides.slideTo(this.segmentsArray.indexOf(event.target.id));
    }

    tihuo(id,cardType){
        this.navCtrl.push(TihuoApplyPage,{
            "id":id,
            "cardType": cardType
        });
    }

    doInfinite(infiniteScroll) {
        console.log('开始请求数据');


        // setTimeout(() => {
        // 	for (let i = 0; i < 6; i++) {
        // 		this.items.push(this.items.length);
        // 	}

        // 	console.log('下拉');
        // 	infiniteScroll.complete();
        // }, 2000);
    } 
}
